import React, { Component } from 'react'
import BaseTableComp from "./BaseTableComp";
import { WeaTools, WeaLocaleProvider } from "ecCom";
import { message } from 'antd'
import Forward from "./Forward";

const { getLabel } = WeaLocaleProvider;
/*
topTitle WeaTop的title
topBtns  WeaTop的btns

tabKey  选项卡的主键
tabUrl  获取tab数据的接口
tabParams 获取tab数据的参数
searchFormUrl 获取高级搜索的接口
searchParams 获取高级搜索的参数
baseSearchName 基础搜索的name
selectedTabKey 直接设置tab页签的selectedTabKey

tableUrl 获取列表数据的接口
tableParams 获取列表数据的参数
getColumns  需要自定义渲染的方法 
onOperatesClick 操作
*/

export default class BaseOdocTypeComp extends Component {

  constructor(props) {
    super(props);
    this.selectedTabKey = (props.location && props.location.query.selectedTabKey) || '0';
    this.state = {
      forwardParams: {},
      showForWard: false,
    }
  }

  render () {
    return (
      <BaseTableComp
        getColumns={this.getColumns}
        searchType={['base', 'advanced']}
        topBtns={this.getTopBtns}
        tabKey='flowtype'
        tabUrl='/api/odoc/odocNewTodoList/getTodoCount'
        searchFormUrl='/api/odoc/odocTodoList/getCustomCondition'
        tableUrl='/api/odoc/odocNewTodoList/getCustomReqList'
        ref={'baseTableComp'}
        onOperatesClick={this.onOperatesClick}
        getOtherDom={this.getOtherDom}
        selectedTabKey={this.selectedTabKey}
        {...this.props}
      />
    )
  }

  getColumns = (columns) => {
    let newColumns = columns.map(column => {
      // if(column.dataIndex == "odoctype"){
      //   return {
      //     ...column, render: (text, record, index) => {
      //       return (<p  class="wea-odoctype-labelp" title={record.odoctypespan}>{record.odoctypespan}</p>);
      //     }
      //   }
      // }else 
      if (column.dataIndex == "wbr") {
        return {
          ...column, render: (text, record) => {
            return (
              <div id={`${record.requestid}todo`}>
                <span style={{ cursor: 'pointer', textDecoration: 'underline' }} onClick={() => this.getTodo(record.requestid, `${record.requestid}todo`)}>{record.wbrspan}</span>
              </div>
            );
          }
        }
      } else if (column.dataIndex == "zbr") {
        return {
          ...column, render: (text, record) => {
            return (
              <div id={`${record.requestid}doing`}>
                <span style={{ cursor: 'pointer', textDecoration: 'underline' }} onClick={() => this.getDoing(record.requestid, `${record.requestid}doing`)}>{record.zbrspan}</span>
              </div>
            );
          }
        }
      } else {
        return column;
      }
    });
    return newColumns;
  }

  onTrack = (selectedKeys, selectedDatas, clear) => {
    const params = {
      requestid: selectedKeys.join(','),
    }
    WeaTools.callApi('/api/odoc/odocNewTodoList/addTracksReq', 'POST', params).then(data => {
      if (data.api_status) {
        message.success(getLabel('517285', '跟踪成功'))
        clear && clear();
        this.refs.baseTableComp.wrappedInstance.getTableDatas();
      }
    })
  }

  getTodo = (requestid, returntdid) => {
    jQuery.ajax({
      url: "/api/odoc/odocNewTodoList/getOperatorPersons?requestid=" + requestid + "&persontype=0",
      type: "post",
      dataType: 'json',
      success: function (res) {
        try {
          jQuery("#" + returntdid).html(res.info);
          jQuery("#" + returntdid).parent().attr("title", res.info)
        } catch (e) { }
      }
    });
  }

  getDoing = (requestid, returntdid) => {
    jQuery.ajax({
      url: "/api/odoc/odocNewTodoList/getOperatorPersons?requestid=" + requestid + "&persontype=1",
      type: "post",
      dataType: 'json',
      success: function (res) {
        try {
          jQuery("#" + returntdid).html(res.info);
          jQuery("#" + returntdid).parent().attr("title", res.info)
        } catch (e) { }
      }
    });
  }

  onOperatesClick = (record, index) => {
    const requestid = record.requestid;
    //跟踪
    if (index == '10') {
      this.onTrack([requestid]);
      //转发  
    } else if (index == '1') {
      let params = {
        requestid: record.requestid,
        nodeid: record.nodeid2,
        showForward: true,
        forwardflag: "1",
        forwardOperators: ''
      };
      this.setState({ forwardParams: params, showForWard: true })
      //打印
    } else if (index == '2') {
      this.doPrintRequest(record.requestid, record.creater);
      //标记已读
    } else if (index == '0') {
      this.doReadRequest(record.requestid);
      //表单日志
    } else if (index == '5') {
      message.warn(getLabel('517286', '目前没有该功能'))
    } else if (index == '11') {
      this.onCancelTrack([requestid]);
    }

  }

  doPrintRequest = (requestid, beluserid, otherpara = '') => {
    const printurl = "/workflow/request/PrintRequestForward.jsp?";
    const urlParams = `requestid=${requestid}&f_weaver_belongto_userid=${beluserid}&f_weaver_belongto_usertype=0${otherpara}`;
    this.openFullWindowHaveBar(printurl + urlParams);
  }


  openFullWindowHaveBar (url) {
    const width = screen.availWidth - 10;
    const height = screen.availHeight - 50;
    let szFeatures = "top=0,left=0,";
    szFeatures += "width=" + width + ",";
    szFeatures += "height=" + height + ",";
    szFeatures += "directories=no,status=yes,toolbar=no,location=no,";
    szFeatures += "menubar=no,scrollbars=yes,resizable=yes";
    window.open(url, "", szFeatures);
  }

  doReadRequest (requestid) {
    WeaTools.callApi('/api/workflow/reqlist/doReadIt', 'POST', { opertype: "single", requestid: requestid }).then(() => {
      this.getTableDatas();
    });
  }

  onCancelTrack (keys, datas, clear) {
    const params = {
      requestid: keys.join(',')
    }
    WeaTools.callApi('/api/odoc/odocNewTodoList/removeTracksReq', 'POST', params).then(data => {
      if (data.api_status) {
        message.success(getLabel('129248', '取消成功'))
        clear && clear();
        this.getTableDatas();
      }
    })
  }

  getOtherDom = () => {
    const { forwardParams, showForWard } = this.state;
    return (
      <div>
        {
          showForWard &&
          <Forward forwardParams={forwardParams} callBackActions={null} />
        }
      </div>
    )
  }

  //重新获取列表
  getTableDatas () {
    this.refs.baseTableComp.wrappedInstance.getTableDatas();
  }

  //重新获取页签
  getTabData () {
    this.refs.baseTableComp.wrappedInstance.getTabData();
  }

  getTopBtns = () => {
    return [
      {
        icon: <i className="icon-coms-currency-Task" />,
        title: getLabel('513369', '批量跟踪'),
        onClick: this.onTrack,
        needCheck: true,
      }
    ]
  }


}

